package com.example.demo.dao;

import com.example.demo.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper  // 重要：这个注解告诉MyBatis这是DAO接口
public interface UserDao {

    // 查询所有用户
    @Select("SELECT * FROM user")
    List<User> findAll();

    // 根据ID查询用户
    @Select("SELECT * FROM user WHERE id = #{id}")
    User findById(Long id);

    // 插入用户
    @Insert("INSERT INTO user(name, age, email, phone) VALUES(#{name}, #{age}, #{email}, #{phone})")
    @Options(useGeneratedKeys = true, keyProperty = "id")  // 获取自增主键
    int insert(User user);

    // 更新用户
    @Update("UPDATE user SET name=#{name}, age=#{age}, email=#{email}, phone=#{phone} WHERE id=#{id}")
    int update(User user);

    // 删除用户
    @Delete("DELETE FROM user WHERE id = #{id}")
    int delete(Long id);

    // 根据姓名查询（模糊查询）
    @Select("SELECT * FROM user WHERE name LIKE CONCAT('%', #{name}, '%')")
    List<User> findByName(String name);

    // 检查邮箱是否存在
    @Select("SELECT COUNT(*) FROM user WHERE email = #{email}")
    int countByEmail(String email);
}